/*
 * VHRpcCallback.java
 * Created on 4 avr. 2009
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.rpc;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Standard callback for asynchronous procedure calls.
 *
 * @param <T> the class of the object that is returned by the an RPC call
 * @version $Id$
 * @author jjanke
 */
public class VHRpcCallback<T> implements AsyncCallback<T>
{
  private final VHRpcResultReceiver<T> d_resultRecipient;
  private final String                 d_strPurpose;

  /**
   * Creates a new VHRpcCallback object.
   *
   * @param resultRecipient the object to which the result of an RPC should be set.
   * @param strPurpose the purpose of the data that is returned to the result recipient
   *          (useful if a recipient receives data for multiple different purposes), may
   *          be <code>null</code> if the receiver only receives data for a single
   *          well-determined purpose
   */
  public VHRpcCallback( VHRpcResultReceiver<T> resultRecipient, String strPurpose )
  {
    d_resultRecipient = resultRecipient;
    d_strPurpose = strPurpose;
  }

  public void onFailure( Throwable t )
  {
    // TODO: better error handling
    Window.alert( t.toString() );
  }

  public void onSuccess( T rpcResult )
  {
    d_resultRecipient.setRpcData( rpcResult, d_strPurpose );
  }
}
